package com.jianhua.appmarket.module.controller;

import com.jianhua.appmarket.common.base.AppResponse;
import com.jianhua.appmarket.module.entity.system.Dept;
import com.jianhua.appmarket.module.entity.system.Dictionary;
import com.jianhua.appmarket.module.entity.system.DictionaryType;
import com.jianhua.appmarket.module.entity.system.Menu;
import com.jianhua.appmarket.module.entity.system.Org;
import com.jianhua.appmarket.module.entity.system.Role;
import com.jianhua.appmarket.module.entity.system.User;
import com.jianhua.appmarket.module.model.system.OrgParam;
import com.jianhua.appmarket.module.model.system.RoleMenuParam;
import com.jianhua.appmarket.module.model.system.RoleUserParam;
import com.jianhua.appmarket.module.model.system.UserParam;
import com.jianhua.appmarket.module.model.system.UserVO;
import com.jianhua.appmarket.module.service.system.IDeptService;
import com.jianhua.appmarket.module.service.system.IDictionaryService;
import com.jianhua.appmarket.module.service.system.IDictionaryTypeService;
import com.jianhua.appmarket.module.service.system.IMenuService;
import com.jianhua.appmarket.module.service.system.IOrgService;
import com.jianhua.appmarket.module.service.system.IRoleMenuService;
import com.jianhua.appmarket.module.service.system.IRoleService;
import com.jianhua.appmarket.module.service.system.IRoleUserService;
import com.jianhua.appmarket.module.service.system.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/system")
public class SystemController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IOrgService orgService;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;

    @Autowired
    private IRoleMenuService roleMenuService;

    @Autowired
    private IRoleUserService roleUserService;

    @Autowired
    private IDictionaryTypeService dictionaryTypeService;

    @Autowired
    private IDictionaryService dictionaryService;

    /***************************用户相关接口******************************************/

    @PostMapping(value = "/user/login")
    public AppResponse<?> login(@RequestBody User user) throws Exception {
        return AppResponse.success(userService.login(user));
    }

    @PostMapping(value = "/user/list")
    public AppResponse<?> listUsers(@RequestBody UserParam param, UserVO user){
        AppResponse<List<UserVO>> response = new AppResponse<>();
        response.setTotal(userService.countUsers(param,user));
        response.setBody(userService.listUsers(param,user));
        return response;
    }

    @PostMapping(value = "/user/save")
    public AppResponse<?> saveUser(@RequestBody UserParam userParam, UserVO user) throws Exception{
        return AppResponse.success(userService.addOrEditUser(userParam,user));
    }

    @PostMapping(value = "/user/delete/{userId}")
    public AppResponse<?> deleteUser(@PathVariable("userId") Long userId,UserVO user){
        return AppResponse.success(userService.deleteUser(userId,user));
    }

    /***************************组织结构接口****x**************************************/

    @PostMapping(value = "/org/list")
    public AppResponse<?> listOrgs(@RequestBody OrgParam param,UserVO user){
        return AppResponse.success(orgService.listOrgsTree(param,user));
    }


    @PostMapping(value = "/org/branch/{parentId}")
    public AppResponse<?> listBranchOrgs(@PathVariable("parentId") Long parentId,UserVO user){
        return AppResponse.success(orgService.listBranchByParentId(parentId,user));
    }

    @PostMapping(value = "/org/save")
    public AppResponse<?> saveOrg(@RequestBody Org org,UserVO user){
        return AppResponse.success(orgService.addOrEditOrg(org,user));
    }

    @PostMapping(value = "/org/delete/{orgId}")
    public AppResponse<?> deleteOrg(@PathVariable("orgId") Long orgId){
        return AppResponse.success(orgService.deleteOrg(orgId));
    }



    /***************************部门接口*********************************************/

    @PostMapping(value = "/dept/list")
    public AppResponse<?> listDepts(UserVO user){
        return AppResponse.success(deptService.listAllDeptByOrgId(user.getOrgId()));
    }

    @PostMapping(value = "/dept/listbyorg/{orgId}")
    public AppResponse<?> listDeptsByOrgId(@PathVariable("orgId") Long orgId){
        return AppResponse.success(deptService.listAllDeptByOrgId(orgId));
    }

    @PostMapping(value = "/dept/branch/{parentId}")
    public AppResponse<?> listBranchDepts(@PathVariable("parentId") Long parentId){
        return AppResponse.success(deptService.listBranchByParentId(parentId));
    }

    @PostMapping(value = "/dept/save")
    public AppResponse<?> saveDept(@RequestBody Dept dept, UserVO user){
        return AppResponse.success(deptService.addOrEditDept(dept,user));
    }

    @PostMapping(value = "/dept/delete/{deptId}")
    public AppResponse<?> deleteDept(@PathVariable("deptId") Long deptId){
        return AppResponse.success(deptService.deleteDept(deptId));
    }



    /***************************角色接口*********************************************/

    @PostMapping(value = "/role/list")
    public AppResponse<?> listRoles(UserVO user){
        return AppResponse.success(roleService.listRolesByOrgId(user.getOrgId()));
    }

    @PostMapping(value = "/role/save")
    public AppResponse<?> saveDept(@RequestBody Role role, UserVO user){
        return AppResponse.success(roleService.addOrEditRole(role,user));
    }

    @PostMapping(value = "/role/delete/{roleId}")
    public AppResponse<?> deleteRole(@PathVariable("roleId") Long roleId){
        return AppResponse.success(roleService.deleteRole(roleId));
    }

    /***************************菜单接口*********************************************/

    @PostMapping(value = "/menu/list")
    public AppResponse<?> listAllMenus(UserVO user){
        return AppResponse.success(menuService.listAllMenus(user));
    }

    @PostMapping(value = "/menu/listbyuser")
    public AppResponse<?> listAllMenusByUser(UserVO user){
        return AppResponse.success(menuService.listMenusByUserId(user.getId()));
    }

    @PostMapping(value = "/menu/branch/{parentId}")
    public AppResponse<?> listBranchMenus(@PathVariable("parentId") Long parentId,UserVO user){
        return AppResponse.success(menuService.listBranchByParentId(parentId,user));
    }

    @PostMapping(value = "/menu/save")
    public AppResponse<?> saveMenu(@RequestBody Menu menu, UserVO user){
        return AppResponse.success(menuService.addOrEditMenu(menu,user));
    }

    @PostMapping(value = "/menu/delete/{menuId}")
    public AppResponse<?> deleteMenu(@PathVariable("menuId") Long menuId){
        return AppResponse.success(menuService.deleteMenu(menuId));
    }


    /***************************权限接口*********************************************/

    @PostMapping(value = "/rolemenu/list/{roleId}")
    public AppResponse<?> listMenus(@PathVariable("roleId") Long roleId){
        return AppResponse.success(roleMenuService.listByRoleId(roleId));
    }

    @PostMapping(value = "/rolemenu/save")
    public AppResponse<?> saveRoleMenus(@RequestBody RoleMenuParam param, UserVO user) throws Exception {
        return AppResponse.success(roleMenuService.saveRoleMenus(param,user));
    }

    @PostMapping(value = "/roleuser/list/{userId}")
    public AppResponse<?> listRolesByUser(@PathVariable("userId") Long userId){
        return AppResponse.success(roleUserService.listByUserId(userId));
    }

    @PostMapping(value = "/roleuser/save")
    public AppResponse<?> saveRoleUsers(@RequestBody RoleUserParam param, UserVO user) throws Exception {
        return AppResponse.success(roleUserService.saveRoleUsers(param,user));
    }

    /***************************字典接口*********************************************/

    @PostMapping(value = "/dictype/list")
    public AppResponse<?> listAllDicType(){
        AppResponse<List<DictionaryType>> response = new AppResponse<>();
        response.setTotal(dictionaryTypeService.countAllDicTypes());
        response.setBody(dictionaryTypeService.listAllDicTypes());
        return response;
    }

    @PostMapping(value = "/dictype/save")
    public AppResponse<?> saveDicType(@RequestBody DictionaryType dictionaryType,UserVO user){
        return AppResponse.success(dictionaryTypeService.saveOrEditDicType(dictionaryType,user));
    }

    @PostMapping(value = "/dic/list/{typeId}")
    public AppResponse<?> listAllDic(@PathVariable("typeId") Long typeId){
        return AppResponse.success(dictionaryService.listAllByTypeId(typeId));
    }

    @PostMapping(value = "/dic/save")
    public AppResponse<?> saveDic(@RequestBody Dictionary param, UserVO user){
        return AppResponse.success(dictionaryService.saveOrEditDic(param,user));
    }

    @PostMapping(value = "/dic/delete/{dicId}")
    public AppResponse<?> saveDic(@PathVariable("dicId") Long dicId){
        return AppResponse.success(dictionaryService.deleteDic(dicId));
    }
}
